RELEASE_NAME?=prometheus-stack
CHART_VERSION?=69.2.0
NAMESPACE?=monitoring
export

.PHONY: prepare_prometheus etcd_certs install_prometheus upgrade_prometheus delete_prometheus template_prometheus upgrade_pushgateway template_pushgateway grafana_ldap

prepare_prometheus:
	-kubectl create ns ${NAMESPACE}
	-helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
	-helm repo update
	-helm show values prometheus-community/kube-prometheus-stack --version ${CHART_VERSION} > ./prometheus-values.yaml

etcd_certs:
	kubectl create secret generic kube-etcd-secret -n ${NAMESPACE} \
		--from-file=ca.crt=/etc/kubernetes/pki/etcd/ca.crt \
		--from-file=ca.key=/etc/kubernetes/pki/etcd/ca.key 

install_prometheus:
	helm install -f prometheus-values.yaml ${RELEASE_NAME} prometheus-community/kube-prometheus-stack --version ${CHART_VERSION} -n ${NAMESPACE}

upgrade_prometheus:
	helm upgrade --install -f prometheus-values.yaml ${RELEASE_NAME} prometheus-community/kube-prometheus-stack --version ${CHART_VERSION} -n ${NAMESPACE}

delete_prometheus:
	-helm uninstall -n ${NAMESPACE} ${RELEASE_NAME}
	-kubectl delete crd alertmanagerconfigs.monitoring.coreos.com -n ${NAMESPACE}
	-kubectl delete crd alertmanagers.monitoring.coreos.com -n ${NAMESPACE}
	-kubectl delete crd podmonitors.monitoring.coreos.com -n ${NAMESPACE}
	-kubectl delete crd probes.monitoring.coreos.com -n ${NAMESPACE}
	-kubectl delete crd prometheusagents.monitoring.coreos.com -n ${NAMESPACE}
	-kubectl delete crd prometheuses.monitoring.coreos.com -n ${NAMESPACE}
	-kubectl delete crd prometheusrules.monitoring.coreos.com -n ${NAMESPACE}
	-kubectl delete crd scrapeconfigs.monitoring.coreos.com -n ${NAMESPACE}
	-kubectl delete crd servicemonitors.monitoring.coreos.com -n ${NAMESPACE}
	-kubectl delete crd thanosrulers.monitoring.coreos.com -n ${NAMESPACE}

template_prometheus:
	helm template -n ${NAMESPACE} ${RELEASE_NAME} prometheus-community/kube-prometheus-stack --debug --version ${CHART_VERSION} > ./build/prometheus_test.yaml